package com.sywl.ytcx.controller.front;

import com.sywl.ytcx.entity.res.ResponseResult;
import com.sywl.ytcx.service.OrderListService;
import com.sywl.ytcx.util.VerTokenUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller("orderListFrontController")
@RestController
@RequestMapping("/api/orderList/front")
public class OrderListController {

    private OrderListService orderListService;
    public OrderListController(OrderListService orderListService) {
        this.orderListService = orderListService;
    }


    /**
     * 获取当前用户的所有订单
     */
    @GetMapping("/myOrders")
    public ResponseResult getMyOrders(@RequestHeader("Authorization") String token) {
        int userId = VerTokenUtil.getUserId(token);
        if (userId == -1) {
            return new ResponseResult("OL400", "无效的token或用户未找到", null);
        }
        return orderListService.getOrdersByUserId(userId);
    }



    /**
     * 获取当前登陆的用户订单（按日期排序）
     * @param token JWT认证令牌
     * @return 统一响应格式
     */
    @GetMapping("/orders/byDate")
    public ResponseResult getOrdersByDate(@RequestHeader("Authorization") String token) {
        int userId = VerTokenUtil.getUserId(token);
        if (userId == -1) {
            return new ResponseResult("OL400", "认证失败", null);
        }
        return orderListService.getOrdersByDate(userId);
    }

    /**
     * 获取当前登陆用户的未付款订单
     * @param token JWT认证令牌
     * @return 统一响应格式
     */
    @GetMapping("/orders/unpaid")
    public ResponseResult getUnpaidOrders(@RequestHeader("Authorization") String token) {
        int userId = VerTokenUtil.getUserId(token);
        if (userId == -1) {
            return new ResponseResult("OL400", "认证失败", null);
        }
        return orderListService.getUnpaidOrders(userId);
    }

    /**
     * 获取前登陆用户的已付款订单
     * @param token JWT认证令牌
     * @return 统一响应格式
     */
    @GetMapping("/orders/paid")
    public ResponseResult getPaidOrders(@RequestHeader("Authorization") String token) {
        int userId = VerTokenUtil.getUserId(token);
        if (userId == -1) {
            return new ResponseResult("OL400", "认证失败", null);
        }
        return orderListService.getPaidOrders(userId);
    }



    /**
     * 获取当前用户今日收入
     * 从请求头 Authorization 中解析 token 获取 userId
     */
    @GetMapping("/dailyIncome")
    public ResponseResult getUserDailyIncome(@RequestHeader("Authorization") String token) {
        int userId = VerTokenUtil.getUserId(token);
        if (userId < 0) {
            return new ResponseResult("OL400", "无效的用户身份", null);
        }
        return orderListService.getUserDailyIncome(userId);
    }

    /**
     * 获取当前用户本月收入
     */
    @GetMapping("/monthlyIncome")
    public ResponseResult getUserMonthlyIncome(@RequestHeader("Authorization") String token) {
        int userId = VerTokenUtil.getUserId(token);
        if (userId < 0) {
            return new ResponseResult("OL400", "无效的用户身份", null);
        }
        return orderListService.getUserMonthlyIncome(userId);
    }


    /**
     * 获取当前用户的所有退款（cancelled）订单
     * 从请求头 Authorization 解析出 userId
     */
    @GetMapping("/cancelled")
    public ResponseResult getUserCancelledOrders(
            @RequestHeader("Authorization") String token) {
        int userId = VerTokenUtil.getUserId(token);
        if (userId < 0) {
            return new ResponseResult("OL400", "无效的用户身份", null);
        }
        return orderListService.getUserCancelledOrders(userId);
    }

    /**
     * 按日期范围查询当前用户的退款订单
     * 支持 yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss
     */
    @GetMapping("/cancelledByDate")
    public ResponseResult getUserCancelledOrdersByDate(
            @RequestHeader("Authorization") String token,
            @RequestParam String dateStart,
            @RequestParam String dateEnd) {
        int userId = VerTokenUtil.getUserId(token);
        if (userId < 0) {
            return new ResponseResult("OL400", "无效的用户身份", null);
        }
        return orderListService.getUserCancelledOrdersByDate(userId, dateStart, dateEnd);
    }


    /**
     * 获取当前用户的帖子转换率
     * @param token 请求头中的 JWT
     * @return ResponseResult.data 为 UserPostConversionStatsVO
     */
    @GetMapping("/user/postStats")
    public ResponseResult getUserPostStats(
            @RequestHeader("Authorization") String token
    ) {
        // 直接使用 Authorization 头的值作为 token
        int userId = VerTokenUtil.getUserId(token);
        if (userId < 0) {
            // token 无效或过期，直接返回 OL400
            return new ResponseResult("OL400", "Token 无效或已过期", null);
        }
        // 调用 Service，传递解析好的 userId
        return orderListService.getUserPostViewOrderRate(userId);
    }



}
